package org.jeecg.modules.bus.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.bus.entity.BusBiddingOffer;
import org.jeecg.modules.bus.entity.BusRecordBiddingVO;
import org.jeecg.modules.bus.entity.BusTenderRecord;
import org.jeecg.modules.bus.entity.*;
import org.jeecg.modules.bus.service.IBusBiddingOfferService;
import org.jeecg.modules.bus.service.IBusTenderApplicationService;
import org.jeecg.modules.bus.service.IBusTenderRecordService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 商务管理（开标记录）
 * @Author: jeecg-boot
 * @Date:   2022-11-15
 * @Version: V1.0
 */
@Api(tags="商务管理（开标记录）")
@RestController
@RequestMapping("/bus/busTenderRecord")
@Slf4j
public class BusTenderRecordController extends JeecgController<BusTenderRecord, IBusTenderRecordService> {
	@Autowired
	private IBusTenderRecordService busTenderRecordService;
	@Autowired
	private IBusBiddingOfferService busBiddingOfferService;
	@Autowired
	private IBusTenderApplicationService busTenderApplicationService;

	/**
	 * 分页列表查询
	 *
	 * @param busTenderRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "商务管理（开标记录）-分页列表查询")
	@ApiOperation(value="商务管理（开标记录）-分页列表查询", notes="商务管理（开标记录）-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BusTenderRecord busTenderRecord,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BusTenderRecord> queryWrapper = QueryGenerator.initQueryWrapper(busTenderRecord, req.getParameterMap());
		Page<BusTenderRecord> page = new Page<BusTenderRecord>(pageNo, pageSize);
		IPage<BusTenderRecord> pageList = busTenderRecordService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param busRecordBiddingVO
	 * @return
	 */
	@AutoLog(value = "商务管理（开标记录）-添加")
	@ApiOperation(value="商务管理（开标记录）-添加", notes="商务管理（开标记录）-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BusRecordBiddingVO busRecordBiddingVO) {
		// 1. 把busTenderRecord保存到开标记录表
		busTenderRecordService.save(busRecordBiddingVO.getBusTenderRecord());
		// 2. 把竞争对手存到竞争对手表当中
		String id = busRecordBiddingVO.getBusTenderRecord().getId();
		List<BusBiddingOffer> busBiddingOfferList = busRecordBiddingVO.getBusBiddingOfferList();
		for (BusBiddingOffer busBiddingOffer : busBiddingOfferList){
			busBiddingOffer.setTenderRecordId(id);
			busBiddingOfferService.save(busBiddingOffer);
		}
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param busTenderRecord
	 * @return
	 */
	@AutoLog(value = "商务管理（开标记录）-编辑")
	@ApiOperation(value="商务管理（开标记录）-编辑", notes="商务管理（开标记录）-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BusTenderRecord busTenderRecord) {
		busTenderRecordService.updateById(busTenderRecord);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商务管理（开标记录）-通过id删除")
	@ApiOperation(value="商务管理（开标记录）-通过id删除", notes="商务管理（开标记录）-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		busTenderRecordService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "商务管理（开标记录）-批量删除")
	@ApiOperation(value="商务管理（开标记录）-批量删除", notes="商务管理（开标记录）-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.busTenderRecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商务管理（开标记录）-通过id查询")
	@ApiOperation(value="商务管理（开标记录）-通过id查询", notes="商务管理（开标记录）-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		//	1.根据id查到开标记录
		BusTenderRecord busTenderRecord = busTenderRecordService.getById(id);
		if(busTenderRecord==null) {
			return Result.error("未找到对应数据");
		}
		// 2.得到开标记录中的投标申请id
		//   根据投标申请id得到投标申记录
		BusTenderApplication busTenderApplication = busTenderApplicationService.getById(busTenderRecord.getTenderApplicationId());

		// 3.根据开标记录id得到竞争对手
		QueryWrapper<BusBiddingOffer> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("tender_record_id",id);
		List<BusBiddingOffer> busBiddingOfferList = busBiddingOfferService.list(queryWrapper);

		BusRecordBiddingVO busRecordBiddingVO = new BusRecordBiddingVO();
		busRecordBiddingVO.setBusTenderRecord(busTenderRecord);
		busRecordBiddingVO.setBusBiddingOfferList(busBiddingOfferList);
		busRecordBiddingVO.setAppApplyTitle(busTenderApplication.getApplyTitle());

		return Result.OK(busRecordBiddingVO);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param busTenderRecord
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BusTenderRecord busTenderRecord) {
        return super.exportXls(request, busTenderRecord, BusTenderRecord.class, "商务管理（开标记录）");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BusTenderRecord.class);
    }

}
